﻿namespace ChinaUnicom.Models
{
    using ChinaUnicom.Exceptions;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Linq;

    public class FeeHistoryCollection : Collection<FeeHistory>, INotifyCollectionChanged, INotifyPropertyChanged
    {
        public FeeHistoryCollection()
        {
            HasMore = false;
        }

        public FeeHistory this[DateTime occurred]
        {
            get { return this.SingleOrDefault(x => x.Occurred == occurred); }
        }

        [JsonIgnore]
        bool hasMore;
        [JsonIgnore]
        public bool HasMore
        {
            get { return hasMore; }
            set
            {
                if (value != this.hasMore)
                {
                    hasMore = value;
                    NotifyPropertyChanged("HasMore");
                }
            }
        }

        public void Add(IEnumerable<FeeHistory> items)
        {
            if (items == null) return;
            foreach (var item in items)
            {
                Add(item);
            }
        }

        public bool Contains(DateTime occurred)
        {
            return this[occurred] != null;
        }

        public bool Remove(DateTime occurred)
        {
            for (int index = 0; index < Count; index++)
            {
                var item = this[index];
                if (item.Occurred == occurred)
                {
                    RemoveItem(index);
                    return true;
                }
            }

            return false;
        }

        protected override void ClearItems()
        {
            base.ClearItems();
            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }

        protected override void InsertItem(int index, FeeHistory item)
        {
            if (item == null)
            {
                throw new UnicomException("Item is null.");
            }

            if (this.Any(h => h.Occurred == item.Occurred))
            {
                // throw new UnicomException("This item has already in the collention.");
                return;
            }

            base.InsertItem(index, item);
            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
            }
        }

        protected override void RemoveItem(int index)
        {
            var item = this[index];
            base.RemoveItem(index);
            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
            }
        }

        protected override void SetItem(int index, FeeHistory item)
        {
            var old = this[index];
            base.SetItem(index, item);
            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, old, index));
            }
        }

        #region INotifyCollectionChanged members
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
